package com.ror.java.se.inet.udp;

//Java使用UDP协议接收数据：
//- 创建接收端的DatagramSocket对象
//- 接收数据包：接收DatagramPacket对象
//- 解析数据包
//- 释放资源

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Demo01UdpReceiver {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket(10086);
        byte[] msgBuf = new byte[1024];
        DatagramPacket dp = new DatagramPacket(msgBuf, msgBuf.length);
        ds.receive(dp);
        System.out.println(new String(dp.getData(), 0, dp.getLength()));
        System.out.println("length=" + dp.getLength());
        System.out.println("address=" + dp.getAddress());
        System.out.println("from-prot=" + dp.getPort());
        ds.close();
    }
}
